package com.ruoyi.web.controller.exam;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.vo.PageResult;
import com.ruoyi.exam.domain.Category;
import com.ruoyi.exam.domain.ExamInfo;
import com.ruoyi.exam.domain.ExamScores;
import com.ruoyi.exam.enums.ExamStatusEnum;
import com.ruoyi.exam.serive.IExamScoreService;
import com.ruoyi.exam.vo.*;
import com.ruoyi.questionBank.domain.ExamPaper;
import com.ruoyi.questionBank.service.IExamPaperService;
import com.ruoyi.subject.SubjectManager;
import com.ruoyi.exam.serive.IExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/exam")
public class ExamController extends BaseController {
    @Autowired
    private IExamService examService;
    @Autowired
    private IExamScoreService scoreService;
    @Autowired
    private IExamPaperService paperService;

    @Value("${ruoyi.exam.frontAddress}")
    private String frontAddress;

    @GetMapping("/paper")
    public AjaxResult getExamPaper(int examId){
        ExamInfo info = examService.getExamInfo(examId);
        if(info != null){
            ExamPaper paper = paperService.get(info.getExamTheory().getPaperId());
//            paper.unloadAnswer();
            return AjaxResult.success(paper);
        }
        return AjaxResult.success();
    }

    @PostMapping("/paper/submit")
    public AjaxResult submitPaper(@RequestBody ExamAnswersVO answersVO){
        answersVO.setUid(getUserId());
        int score = examService.submitAnswer(answersVO);
        return AjaxResult.success(score);
    }

    @PostMapping("/handsOn/submit")
    public AjaxResult submit(@RequestBody ExamScoreSubmitVO scoreVO){
        scoreVO.setUid(getUserId());
        scoreService.submitHandsOnScore(scoreVO);
        return AjaxResult.success();
    }

    @PostMapping("/theory/submit")
    public AjaxResult submitTheory(@RequestBody ExamScoreSubmitVO scoreVO){
        scoreVO.setUid(getUserId());
        scoreService.submitTheoryScore(scoreVO);
        return AjaxResult.success();
    }

    /***
     * 获取最新一场考试
     * @return
     */
    @GetMapping("/last")
    public AjaxResult getLastExam(){
        AjaxResult result = AjaxResult.success();
        ExamInfo examInfo = examService.getLastExam();
        if(examInfo.getStatus() == ExamStatusEnum.IN_PROGRESS.getStatus()) {
            if (UserType.GENERAL.getValue().equals(SecurityUtils.getUserType())) {
                boolean inExam = false;
                long uid = getUserId();
                ExamScores scores = scoreService.get(uid, examInfo.getId());
                if(scores == null){
                    result.put("handsOn" , true);
                    result.put("theory" , true);
                    setExamUrl(result);
                    inExam = true;
                }else{
                    if(scores.getHandsOnScore() == null){
                        result.put("handsOn" , true);
                        inExam = true;
                    }
                    if(scores.getTheoryScore() == null){
                        result.put("theory" , true);
                        setExamUrl(result);
                        inExam = true;
                    }
                }
                if(inExam){
                    result.put(AjaxResult.DATA_TAG ,examInfo);
                }
            }
        }
        return result;
    }

    private void setExamUrl(AjaxResult ajaxResult){
        ajaxResult.put("url" ,frontAddress + "/exam/last");
    }

    @GetMapping("/ranking")
    public TableDataInfo rankingList(ExamRankingReqVO rankingReqVo){
        List<ExamRankingData> rankingData = scoreService.listRankingData(rankingReqVo);
        return getDataTable(rankingData);
    }

    @PreAuthorize("@ss.hasPermi('exam:list')")
    @GetMapping("/list")
    public TableDataInfo page(ExamInfoSearchVO searchVO){
        PageResult<ExamInfo> page = examService.getPage(searchVO);

        return getDataTable(page.convertToPage());
    }

    @PreAuthorize("@ss.hasPermi('exam:add')")
    @PostMapping("/add")
    public AjaxResult insertExam(@RequestBody ExamInfo info){
        examService.addExam(info);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('exam:update')")
    @PostMapping("/update")
    public AjaxResult updateExam(@RequestBody ExamInfo info){
        examService.updateExam(info);
        info = examService.getExamInfo(info.getId());
        return AjaxResult.success(info);
    }
}
